package com.coollang.smartbra.presenter;

import android.widget.Toast;

import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.http.ResponseData;
import com.coollang.smartbra.utils.MD5Utils;
import com.coollang.smartbra.utils.TimeUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.lang.reflect.ParameterizedType;
import java.util.Map;

import io.realm.Realm;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Timmy
 *         p层的基类
 */
public abstract class BasePresenter<T> {

    public static final String TAG = BasePresenter.class.getSimpleName();
    public final Gson mGson;
    public Realm mRealm;

    private final Class<T> mDataType;

    // 接口参数
    protected final String PARAMS_1 = "timestamp";
    protected final String PARAMS_2 = "sign";


    public abstract void setRequestInfo();

    public BasePresenter(Class<T> dataType) {
        mGson = new Gson();

        try {
            mRealm = Realm.getDefaultInstance();
        } catch (RuntimeException e) {
            Realm.deleteRealm(Realm.getDefaultConfiguration());
            mRealm = Realm.getDefaultInstance();
        }

        this.mDataType = dataType;
        if (!ResponseData.class.isAssignableFrom(dataType)) {
            throw new IllegalArgumentException("传入的class必须实现ResponseData接口");
        }

        if (dataType.getGenericSuperclass() instanceof ParameterizedType && ((ParameterizedType) dataType.getGenericSuperclass()).getActualTypeArguments().length > 0) {
            throw new IllegalArgumentException("传入的class参数不能带泛型");
        }
    }

    public void setBaseOkgo(String url) {
        String timeStamp = TimeUtils.getCurrentTimeUnixString();
        String sign = MD5Utils.getMD5(url.split("/")[1] + timeStamp);

        OkGo.get(Constants.HOST + url)
                .params(PARAMS_1, timeStamp)
                .params(PARAMS_2, sign)
                .params(getData(), false)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, okhttp3.Response response) {
                        T t = mGson.fromJson(s, mDataType);
                        parsrData(t);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        e.printStackTrace();
                        Toast.makeText(MyApplication.getContext(), "请检查当前网络", Toast.LENGTH_SHORT).show();
                        onRequestError();
                    }
                });
    }

    public void setBaseOkgo1(String url) {
        String timeStamp = TimeUtils.getCurrentTimeUnixString();
        String sign = MD5Utils.getMD5(url.split("/")[1] + timeStamp);

        OkGo.get(Constants.HOST + url)
                .params(PARAMS_1, timeStamp)
                .params(PARAMS_2, sign)
                .params(getData(), false)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, okhttp3.Response response) {
                        parsrData(s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        e.printStackTrace();
                        Toast.makeText(MyApplication.getContext(), "请检查当前网络", Toast.LENGTH_SHORT).show();
                        onRequestError();
                    }
                });
    }


    protected abstract Map<String, String> getData();

    /**
     * 交由子类去解析数据
     */
    protected abstract void parsrData(T t);


    protected void parsrData(String data) {
    }

    protected void onRequestError() {

    }
}
